package com.xiaoxiao.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xiaoxiao.constant.Constant;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainLiveKeeper extends Activity {
	
	private ListView listView;

	private Button button;
	
	private SimpleAdapter simpleAdapter;
	
//	private Intent intent;
    
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_livekeeper);
		
	    simpleAdapter = new SimpleAdapter(this, getData(),
				R.layout.item_listview, new String[] {"ItemImage", "title", "info" },
				new int[] {R.id.ItemImage, R.id.title, R.id.info });
		listView = (ListView) findViewById(R.id.livekeeper_list);
		listView.setAdapter(simpleAdapter);
		//监听 listView的长按事件
		listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {		
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				//却换到detail
				forwardDetail();
			}
			
		});
		

		button = (Button) findViewById(R.id.btn_add);
		// 监听“添加消费信息”button
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				openDialogForAddSpendInfo();
			}
		});

	}
	
	//“添加消费信息”界面
	private void openDialogForAddSpendInfo(){  
		LayoutInflater layoutInflater = LayoutInflater.from(this);
        View addSpendInfoView = layoutInflater.inflate(R.layout.spend_form, null);
        
        AlertDialog.Builder builder = new AlertDialog.Builder(this);     
        builder.setTitle(Constant.SPEND_TITLE);  
        builder.setView(addSpendInfoView);
        builder.setPositiveButton(Constant.SAVE, null);
        builder.show();  
    }   
	
	private void forwardDetail(){
		Intent intent  = this.getIntent();
		intent.setClass(MainLiveKeeper.this, DetailLiveKeeper.class);
		startActivity(intent);
		finish();
	}


	@SuppressWarnings("unused")
	private List<String> mockListData() {
		List<String> list = new ArrayList<String>();
		list.add("2011-03-10消费：30");
		list.add("2011-03-10消费：30");
		list.add("2011-03-10消费：30");
		list.add("2011-03-10消费：30");
		return list;
	}

	private List<Map<String, Object>> getData() {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", "1.");
		map.put("ItemImage", R.drawable.icon);
		map.put("info", "2011-03-11 总费用： 30");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("title", "2.");
		map.put("ItemImage",  R.drawable.icon);
		map.put("info", "2011-03-11 总费用： 30");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("title", "3.");
		map.put("ItemImage",  R.drawable.icon);
		map.put("info", "2011-03-11 总费用： 30");
		list.add(map);

		return list;

	}
	
	private List<Map<String, Object>> getDataForDeatil() {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("txt_time", "2011-03-11");
		map.put("txt_type", "住");	
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("txt_time", "2011-03-11");
		map.put("txt_type", "住");	
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("txt_time", "2011-03-11");
		map.put("txt_type", "住");	
		list.add(map);

		return list;

	}

}
